---
title: "Cleaning Caucasus Barometer"
---

# Load

```{r}
# load packages
  source("helper-packages.R")

# load georgia 2019
  ga_2019_raw <- 
    read_dta("../raw-data/y-multi-caucasus-barometer/caucasus-barometer-2019-georgia/CB2019_Georgia_response_30Jan2020.dta")

# load armenia 2019
  ar_2019_raw <- 
    read_dta("../raw-data/y-multi-caucasus-barometer/caucasus-barometer-2019-armenia/cb2019_CB_2019_Armenia_responses.dta")
    
# load georgia 2017
  ga_2017_raw <- 
    read_dta("../raw-data/y-multi-caucasus-barometer/caucasus-barometer-2017-georgia/CB_2017_Georgia_public_17.11.17.dta")

# load armenia 2017
  ar_2017_raw <- 
    read_dta("../raw-data/y-multi-caucasus-barometer/caucasus-barometer-2017-armenia/CB_2017_Armenia_public.dta")

# load georgia/armenia 2015
  ga_ar_2015_raw <- 
    read_dta("../raw-data/y-multi-caucasus-barometer/caucasus-barometer-2015-georgia-armenia/CB_2015_Regional_Only_Responses_140416.dta")

# load georgia/armenia/azerbaijan 2013
  ga_ar_az_2013_raw <- 
    read_dta("../raw-data/y-multi-caucasus-barometer/caucasus-barometer-2013-georgia-armenia-azerbaijan/CB2013_Regional_only_responses_07032014.dta")
    
# load georgia/armenia/azerbaijan 2012
  ga_ar_az_2012_raw <- 
    read_dta("../raw-data/y-multi-caucasus-barometer/caucasus-barometer-2012-georgia-armenia-azerbaijan/CB2012_Regional_only_responses_01032013.dta")

# load georgia/armenia/azerbaijan 2011
  ga_ar_az_2011_raw <- 
    read_dta("../raw-data/y-multi-caucasus-barometer/caucasus-barometer-2011-georgia-armenia-azerbaijan/CB2011_Regional_only_responses_1202012.dta")
  
# load georgia/armenia/azerbaijan 2010
  ga_ar_az_2010_raw <- 
    read_dta("../raw-data/y-multi-caucasus-barometer/caucasus-barometer-2010-georgia-armenia-azerbaijan/CB2010_Regional_only_responses_21032011.dta")

# load georgia/armenia/azerbaijan 2009
  ga_ar_az_2009_raw <- 
    read_dta("../raw-data/y-multi-caucasus-barometer/caucasus-barometer-2009-georgia-armenia-azerbaijan/CB2009_Regional_only_responses_18012011.dta")
```

# Georgia 2020

-NOT PROCESSED:
--REASONS:
---No religious intolerance question (short phone survey due to Covid)

# Extract and rename variables from all surveys

* Note the surveys are broadly consistent, so it's efficient to perform the cleaning on a stacked dataframe
* Social distance questions about Molokans were only asked in Armenia.

```{r}
# 2019 georgia
  n1_ga_2019_clean <- 
    ga_2019_raw %>% 
    select(
      INTDATE = INT_DATE,
      STRATUM,
      RELGION,
      RESPAGE,
      RESPEDU,
      RESPSEX,
      NEIGHBOR,
      BUSINJEW,
      MARWJEW,
      GALLTRU, # trust
      RELIMP, # religiosity (how important)
      ) %>% 
    mutate(
      resp_country_original = "Georgia",
      resp_round = "2019",
      RELGION = to_character(RELGION))

# 2019 armenia
  n2_ar_2019_clean <- 
    ar_2019_raw %>% 
    select(
      COUNTRY,
      INTDATE = INT_DATE,
      STRATUM,
      RELGION = RELGNEW, # note, 10 categories
      RESPAGE,
      RESPEDU, # same
      RESPSEX,
      NEIGHBOR,
      BUSINJEW,
      MARWJEW,
      BUSINMOL, # doing business with a Molokan
      MARWMOL, # marrying a Molokan
      BUSINJW, # doing business with a Jehovah’s Witness
      MARWJW, # marrying a Jehovah’s Witness
      GALLTRU, # trust
      RELSERV # religiosity (attend services; RELIMP in codebook but not in data set)
      ) %>% 
    mutate(
      resp_country_original = 
        # only Armenia is in the data set
        case_when(
          COUNTRY == 1 ~ "Armenia",
          COUNTRY == 2 ~ "Azerbaijan",
          COUNTRY == 3 ~ "Georgia"),
      resp_round = "2019",
      RELGION = to_character(RELGION))
  
# 2017 georgia
  n3_ga_2017_clean <- 
    ga_2017_raw %>% 
    select(
      COUNTRY,
      INTDATE = INT_DATE,
      STRATUM,
      RELGION = RELGNEW, # note, 10 categories
      RESPAGE = AGE,
      RESPEDU, # same
      RESPSEX,
      NEIGHBOR,
      BUSINJEW,
      MARWJEW,
      GALLTRU, # trust
      RELSERV # different religiosity question (attend services)
      ) %>% 
    mutate(
      resp_country_original = 
        case_when(
          COUNTRY == 1 ~ "Armenia",
          COUNTRY == 2 ~ "Azerbaijan",
          COUNTRY == 3 ~ "Georgia"),
      resp_round = "2017",
      RELGION = to_character(RELGION))
  
# 2017 armenia
  n4_ar_2017_clean <- 
    ar_2017_raw %>% 
    select(
      COUNTRY,
      INTDATE = INT_DATE,
      STRATUM,
      RELGION = RELGNEW, # note, 10 categories
      RESPAGE = AGE,
      RESPEDU, # same
      RESPSEX,
      NEIGHBOR,
      BUSINJEW,
      MARWJEW,
      BUSINMOL, # doing business with a Molokan
      MARWMOL, # marrying a Molokan
      GALLTRU, # trust
      RELSERV # different religiosity question (attend services)
      ) %>% 
    mutate(
      resp_country_original = 
        case_when(
          COUNTRY == 1 ~ "Armenia",
          COUNTRY == 2 ~ "Azerbaijan",
          COUNTRY == 3 ~ "Georgia"),
      resp_round = "2017",
      RELGION = to_character(RELGION))
    
# 2015
  n5_ga_ar_2015_clean <- 
    ga_ar_2015_raw %>% 
    select(
      COUNTRY,
      INTDATE,
      STRATUM,
      RELGION = RELGNEW, # note, 10 categories
      RESPAGE = AGE,
      RESPEDU, # same
      RESPSEX,
      BUSINJEW,
      MARWJEW,
      BUSINMOL, # doing business with a Molokan
      MARWMOL, # marrying a Molokan
      GALLTRU, # trust
      RLGIOUS # different religiosity question (how religious)
      ) %>% 
    mutate(
      resp_country_original = 
        case_when(
          COUNTRY == 1 ~ "Armenia",
          COUNTRY == 2 ~ "Azerbaijan",
          COUNTRY == 3 ~ "Georgia"),
      resp_round = "2015",
      RELGION = to_character(RELGION))

# 2013
  n6_ga_ar_az_2013_clean <- 
    ga_ar_az_2013_raw %>% 
    select(
      COUNTRY,
      INTDATE,
      STRATUM,
      RELGION = RELGNEW, # note, 10 categories
      RESPAGE,
      RESPEDU, # same
      RESPSEX,
      BUSINJEW,
      MARWJEW,
      BUSINMOL, # doing business with a Molokan
      MARWMOL, # marrying a Molokan
      GALLTRU, # trust
      RELIMP # religiosity (how important)
      ) %>% 
    mutate(
      resp_country_original = 
        case_when(
          COUNTRY == 1 ~ "Armenia",
          COUNTRY == 2 ~ "Azerbaijan",
          COUNTRY == 3 ~ "Georgia"),
      resp_round = "2013",
      RELGION = to_character(RELGION))    
  
# 2012
  n7_ga_ar_az_2012_clean <- 
    ga_ar_az_2012_raw %>% 
    select(
      COUNTRY,
      INTDATE,
      STRATUM,
      RELGION = RELGNEW, # note, 10 categories
      RESPAGE,
      RESPEDU, # same
      RESPSEX,
      BUSINJEW,
      MARWJEW, 
      FEELTRU = FEELTRUN, # different trust question (statement) # standardize the name
      RLGIOUS # different religiosity question (how religious)
      ) %>% 
    mutate(
      resp_country_original = 
        case_when(
          COUNTRY == 1 ~ "Armenia",
          COUNTRY == 2 ~ "Azerbaijan",
          COUNTRY == 3 ~ "Georgia"),
      resp_round = "2012",
      RELGION = to_character(RELGION))    
  
# 2011
  n8_ga_ar_az_2011_clean <- 
    ga_ar_az_2011_raw %>% 
    select(
      COUNTRY,
      INTDATE = INT_DATE,
      STRATUM,
      RELGION = RELGNEW, # note, 10 categories
      RESPAGE = AGE,
      RESPEDU, # same
      RESPSEX,
      BUSINJEW,
      MARWJEW,
      GALLTRU, # trust
      RELIMP # religiosity (how important)
      ) %>% 
    mutate(
      resp_country_original = 
        case_when(
          COUNTRY == 1 ~ "Armenia",
          COUNTRY == 2 ~ "Azerbaijan",
          COUNTRY == 3 ~ "Georgia"),
      resp_round = "2011",
      RELGION = to_character(RELGION))    

# 2010
  n9_ga_ar_az_2010_clean <- 
    ga_ar_az_2010_raw %>% 
    select(
      COUNTRY,
      INTDATE,
      STRATUM,
      RELGION, # note, 8 categories
      RESPAGE,
      RESPEDU, # same
      RESPSEX,
      BUSINJEW,
      MARWJEW,
      GALLTRU, # trust
      RELIMP # religiosity (how important)
      ) %>% 
    mutate(
      resp_country_original = 
        case_when(
          COUNTRY == 1 ~ "Armenia",
          COUNTRY == 2 ~ "Azerbaijan",
          COUNTRY == 3 ~ "Georgia"),
      resp_round = "2010",
      RELGION = to_character(RELGION))  
  
# 2009
  n10_ga_ar_az_2009_clean <- 
    ga_ar_az_2009_raw %>% 
    select(
      COUNTRY,
      INTDATE,
      STRATUM,
      RELGION, # note, 9 categories
      RESPAGE,
      RESPEDU, # same
      RESPSEX,
      FRNDJEW,
      BUSINJEW,
      MARWJEW,
      FEELTRU, # different trust question (statement)
      RELSERV # different religiosity question (attend services)
      ) %>% 
    mutate(
      resp_country_original = 
        case_when(
          COUNTRY == 1 ~ "Armenia",
          COUNTRY == 2 ~ "Azerbaijan",
          COUNTRY == 3 ~ "Georgia"),
      resp_round = "2009",
      RELGION = to_character(RELGION))
```

# Stack subpart dataframes

```{r}
  stacked_raw <- 
    n1_ga_2019_clean %>% 
    bind_rows(n2_ar_2019_clean) %>% 
    bind_rows(n3_ga_2017_clean) %>% 
    bind_rows(n4_ar_2017_clean) %>% 
    bind_rows(n5_ga_ar_2015_clean) %>% 
    bind_rows(n6_ga_ar_az_2013_clean) %>% 
    bind_rows(n7_ga_ar_az_2012_clean) %>% 
    bind_rows(n8_ga_ar_az_2011_clean) %>% 
    bind_rows(n9_ga_ar_az_2010_clean) %>% 
    bind_rows(n10_ga_ar_az_2009_clean)
```

# Clean stacked

```{r}
# clean
  stacked_clean <- 
    stacked_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Caucasus Barometer",
        
      # round number (character vector, title case)  
        resp_round = resp_round,
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "bit.ly/3LVCeAm",

      # survey mode (in-person/phone/internet)
        resp_survey_mode = "in-person",    

      # country (character vector; list of countries as written in original source)
        resp_country_original = resp_country_original,

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = INTDATE,
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
      # respondent's religion (character vector that corresponds to master list)
        resp_religion = 
          case_when(
            RELGION %in% c(
              "Orthodox Church",
              "Armenian Apostolic Church",
              "Protestant Church",
              "Roman Catholic Church",
              "Other Christian Church or group",
              "Georgian, Russian or Greek Orthodox Church",
              "Protestant church",
              "Other Christian Church or Sect",
              "Other Protestant Church"
              ) ~ "Christian",
            RELGION %in% c(
              "Sunni Islam",
              "Other Islamic branches",
              "Shia Islam",
              "Islam"
              ) ~ "Muslim",
            RELGION %in% c(
              "Judaism"
              ) ~ "Jewish",
            RELGION %in% c(
              "Other",
              "Break off"
              ) ~ "Other religion",
            TRUE ~ NA_character_),

      # respondent's religion (character vector that corresponds to master list)
        resp_denomination = RELGION,      
      
      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age =
          dplyr::recode(
            as.character(RESPAGE),
            "-3" = NA_character_,
            "-1" = NA_character_
          ),        
      
      # respondent's education level
        resp_education_original =
          dplyr::recode(
            as.character(RESPEDU),
            "1" = "1. No primary education [No education]",
            "2" = "2. Primary education (either complete or incomplete) [Primary]",
            "3" = "3. Incomplete secondary education [Primary]",
            "4" = "4. Completed secondary education [Primary]",
            "5" = "5. Secondary technical education [Primary]",
            "6" = "6. Incomplete higher education [Primary]",
            "7" = "7. Completed higher education (BA, MA, or Specialist degree) [College]",
            "8" = "8. Post-graduate degree [College]",
            .default = NA_character_),       
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female = 
          case_when(
            RESPSEX == 2 ~ 1,
            RESPSEX == 1 ~ 0,
            TRUE ~ NA_real_),

      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural = 
          case_when(
            STRATUM == 3 ~ 1,
            STRATUM %in% c(1,2) ~ 0,
            TRUE ~ NA_real_),
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: NEIGHBOR; QTEXT: Which of these people would you not wish to have as your neighbors most?; ROPTIONS: 1 = People following a religion that is different to yours [=1] + 2 = People having politics views that are different to yours [=0] + 3 = Europeans who come to live in /country/ and want to stay [=0] + 4 = Asians who come to live in /country/ and want to stay [=0] + 5 = Drug addicts [=0] + 6 = Homosexuals [=0] + 7 = Black people [=0] + 8 = Criminals [=0] + 9 = Other, specify [=0] + 10/-5 = I would not wish any of these people as my neighbors [=1]; TARGET: Different religion; TYPE: Distance, neighbor",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(NEIGHBOR),
            "-1" = NA_character_,
            "-2" = NA_character_,
            "-3" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            NEIGHBOR %in% c(1, 10, -5) ~ 1,
            NEIGHBOR %in% c(2:9) ~ 0,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: BUSINJEW; QTEXT: Now I am going to read out a list of nationalities. Can you please tell me whether you approve or disapprove of people of your ethnicity doing business with a Jew.; ROPTIONS: 1 = Approve [=0] + 0 = Disapprove [=1]; TARGET: Jewish; TYPE: Distance, work",
      
      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(BUSINJEW),
            "-9" = NA_character_,
            "-3" = NA_character_,
            "-2" = NA_character_,
            "-1" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            BUSINJEW %in% c(0) ~ 1,
            BUSINJEW %in% c(1) ~ 0,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: MARWJEW; QTEXT: Would you approve or disapprove of women of your ethnicity marrying a Jew?; ROPTIONS: 1 = Approve [=0] + 0 = Disapprove [=1]; TARGET: Jewish; TYPE: Distance, family",
      
      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(MARWJEW),
            "-3" = NA_character_,
            "-2" = NA_character_,
            "-1" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            MARWJEW %in% c(0) ~ 1,
            MARWJEW %in% c(1) ~ 0,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 4 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_4_qinfo = "NUM: FRNDJEW; QTEXT: Would you approve or disapprove of being friends with Jews?; ROPTIONS: 1 = Approve [=0] + 0 = Disapprove [=1]; TARGET: Jewish; TYPE: Distance, friend",
      
      # original response (as character vector)
        resp_soc_dist_4_original = 
          dplyr::recode(
            as.character(FRNDJEW),
            "-3" = NA_character_,
            "-2" = NA_character_,
            "-1" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_4_bin_recode = 
          case_when(
            FRNDJEW %in% c(0) ~ 1,
            FRNDJEW %in% c(1) ~ 0,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 5 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_5_qinfo = "NUM: BUSINMOL; QTEXT: Can you please tell me whether you approve or disapprove of people of your ethnicity doing business with a Molokan.; ROPTIONS: 1 = Approve [=0] + 0 = Disapprove [=1]; TARGET: Molokan; TYPE: Distance, work",
      
      # original response (as character vector)
        resp_soc_dist_5_original = 
          dplyr::recode(
            as.character(BUSINMOL),
            "-5" = NA_character_,
            "-3" = NA_character_,
            "-2" = NA_character_,
            "-1" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_5_bin_recode = 
          case_when(
            BUSINMOL %in% c(0) ~ 1,
            BUSINMOL %in% c(1) ~ 0,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 6 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_6_qinfo = "NUM: MARWMOL; QTEXT: Would you approve or disapprove of women of your ethnicity marrying a Molokan?; ROPTIONS: 1 = Approve [=0] + 0 = Disapprove [=1]; TARGET: Molokan; TYPE: Distance, family",
      
      # original response (as character vector)
        resp_soc_dist_6_original = 
          dplyr::recode(
            as.character(MARWMOL),
            "-5" = NA_character_,
            "-3" = NA_character_,
            "-2" = NA_character_,
            "-1" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_6_bin_recode = 
          case_when(
            MARWMOL %in% c(0) ~ 1,
            MARWMOL %in% c(1) ~ 0,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 7 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_7_qinfo = "NUM: BUSINJW; QTEXT: Can you please tell me whether you approve or disapprove people like you doing business with a Jehovah’s Witness?; ROPTIONS: 1 = Yes (sic, means approve) [=0] + 0 = No (sic, means disapprove) [=1]; TARGET: Jehovahs Witness; TYPE: Distance, work",
      
      # original response (as character vector)
        resp_soc_dist_7_original = 
          dplyr::recode(
            as.character(BUSINJW),
            "-2" = NA_character_,
            "-1" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_7_bin_recode = 
          case_when(
            BUSINJW %in% c(0) ~ 1,
            BUSINJW %in% c(1) ~ 0,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 8 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_8_qinfo = "NUM: MARWJW; QTEXT: Can you please tell me whether you approve or disapprove people like you marrying a Jehovah’s Witness?; ROPTIONS: 1 = Yes (sic, means approve) [=0] + 0 = No (sic, means disapprove) [=1]; TARGET: Jehovahs Witness; TYPE: Distance, family",
      
      # original response (as character vector)
        resp_soc_dist_8_original = 
          dplyr::recode(
            as.character(MARWJW),
            "-2" = NA_character_,
            "-1" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_8_bin_recode = 
          case_when(
            MARWJW %in% c(0) ~ 1,
            MARWJW %in% c(1) ~ 0,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = 
          case_when(
            
            resp_round == 2009 ~ "NUM: N1 [FEELTRU]; QTEXT: For each of the following statements, please tell me whether it describes your feelings, more or less describes your feelings, or does not describe your feelings. There are many people I can trust completely.; ROPTIONS: 1 = Does not describe [=1] + 2 = More or less describes [=0] + 3 = Describes [=0]",
            
            resp_round == 2012 ~ "NUM: N2 [FEELTRUN]; QTEXT: To what extent each of the following statements describes your feelings? Please use this CARD, where ‘1’ means 'Does not describe' and '10' means 'Describes.' There are many people I can trust completely.; ROPTIONS: 1(/2/3/4) = Does not describe [=1] + (5/6/7/8/9/)10 = Describes [=0]",
            
            TRUE ~ "NUM: N2/N3/N6/N7 [GALLTRU]; QTEXT: Generally speaking, would you say that most people in (country) can be trusted, or that you can't be too careful in dealing with people?; ROPTIONS: 1(/2/3/4) = You can't be too careful [=1] + (5/6/7/8/9/)10 = Most people can be trusted [=0]"),

      # original response (as character vector)
        resp_gentrust_original = 
          case_when(
            # 2009
            resp_round == 2009 ~ as.character(FEELTRU),
            resp_round == 2009 & FEELTRU < 0 ~ NA_character_,
            
            # 2012
            resp_round == 2012 ~ as.character(FEELTRU),
            resp_round == 2012 & FEELTRU < 0 ~ NA_character_,
            
            # rest of the rounds
            !resp_round %in% c(2009, 2012) ~ as.character(GALLTRU),
            !resp_round %in% c(2009, 2012) & GALLTRU < 0 ~ NA_character_
          ),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = 
          case_when(
            # 2009
            resp_round == 2009 & FEELTRU %in% c(2:3) ~ 0,
            resp_round == 2009 & FEELTRU == 1 ~ 1,
            
            # 2012
            resp_round == 2012 & FEELTRU < 5 ~ 1,
            resp_round == 2012 & FEELTRU >= 5 ~ 0,
            
            # rest of the rounds
            !resp_round %in% c(2009, 2012) & GALLTRU < 5 ~ 1,
            !resp_round %in% c(2009, 2012) & GALLTRU >= 5 ~ 0,
            
            TRUE ~ NA_real_),    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = 
          case_when(
            # 2019 Georgia: importance
            resp_round == 2019 & resp_country_original == "Georgia" ~ "NUM: D14 [RELIMP]; QTEXT:  How important is religion in your daily life? Please use this CARD.; ROPTIONS: 1 = Not at all important + 2 = Not very important + 3 = Rather important + 4 = Very important",
            # 2019 Armenia: services
            resp_round == 2019 & resp_country_original == "Armenia" ~ "NUM: D12 [RELSERV]; QTEXT: Apart from special occasions such as weddings and funerals, about how often do you attend religious services nowadays? Please use this CARD.; ROPTIONS: 1 = Everyday + 2 = More than once a week + 3 = Once a week + 4 = At least once a month + 5 = Only on special religious holidays + 6 = Less often + 7 = Never",
            # importance for waves other than 2019 Georgia 
            resp_round %in% c(2013, 2011, 2010) ~ "NUM: D13/D17 [RELIMP]; QTEXT:  How important is religion in your daily life? Please use this CARD.; ROPTIONS: 1 = Not at all important + 2 = Not very important + 3 = Rather important + 4 = Very important",
            # services for waves other than 2019 Armenia
            resp_round %in% c(2017, 2009) ~ "NUM: D12/D13 [RELSERV]; QTEXT: Apart from special occasions such as weddings and funerals, about how often do you attend religious services nowadays? Please use this CARD.; ROPTIONS: 1 = Everyday + 2 = More than once a week + 3 = Once a week + 4 = At least once a month + 5 = Only on special religious holidays + 6 = Less often + 7 = Never",
            # religious
            resp_round %in% c(2015, 2012) ~ "NUM: D14/D16 [RLGIOUS]; QTEXT: Overall, how religious would you say you are? Please use this CARD, where code ‘1’ means 'I’m not religious at all' and code ‘10’ means 'I’m very religious.'; ROPTIONS: 1 = Not religious at all + 10 = Very religious"
            ),
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        temp_resp_religiosity_original = 
          case_when(
            # 2019 Georgia: importance
            resp_round == 2019 & resp_country_original == "Georgia" ~ as.numeric(RELIMP),
            resp_round == 2019 & resp_country_original == "Georgia" & RELIMP %in% c(-9, -7, -3, -2, -1) ~ NA_real_, 
            # 2019 Armenia: services
            resp_round == 2019 & resp_country_original == "Armenia" ~ as.numeric(RELSERV),
            resp_round == 2019 & resp_country_original == "Armenia" & RELSERV %in% c(-7, -3, -2, -1) ~ NA_real_,
            # importance for waves other than 2019 Georgia
            resp_round %in% c(2013, 2011, 2010) ~ as.numeric(RELIMP),
            resp_round %in% c(2013, 2011, 2010) & RELIMP %in% c(-9, -7, -3, -2, -1) ~ NA_real_,
            # services for waves other than 2019 Armenia
            resp_round %in% c(2017, 2009) ~ as.numeric(RELSERV),
            resp_round %in% c(2017, 2009) & RELSERV %in% c(-7, -3, -2, -1) ~ NA_real_,
            # religious
            resp_round %in% c(2015, 2012) ~ as.numeric(RLGIOUS),
            resp_round %in% c(2015, 2012) & RLGIOUS %in% c(-7, -3, -2, -1) ~ NA_real_,
            
            TRUE ~ NA_real_
          ), 

      resp_religiosity_original =
        case_when(
          temp_resp_religiosity_original < 0 ~ NA_real_,
          TRUE ~ temp_resp_religiosity_original
      ),

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode =
          case_when(
            # 2019 Georgia: importance
            resp_round == 2019 & resp_country_original == "Georgia" ~ (resp_religiosity_original - 1)/3,
            # 2019 Armenia: services
            resp_round == 2019 & resp_country_original == "Armenia" ~ (7 - resp_religiosity_original)/6,
            # importance for waves other than 2019 Georgia 
            resp_round %in% c(2013, 2011, 2010) ~ (resp_religiosity_original - 1)/3,
            # services for waves other than 2019 Armenia
            resp_round %in% c(2017, 2009) ~ (7 - resp_religiosity_original)/6,
            # religious
            resp_round %in% c(2015, 2012) ~ (resp_religiosity_original - 1)/9
          )
    
    ) %>% 
    select(starts_with("resp_"))
```

# Add missing dates (incorrect dates in original; impute minimum and maximum dates for that county/round)

```{r}
# find date ranges
  #stacked_clean %>% 
     #filter(resp_country_common == "Georgia") %>% 
     #filter(resp_round == "2010") %>%
     #filter(resp_interview_date != "1959-12-29") %>%
     #pull(resp_interview_date) %>% 
     #range(na.rm = T)
  
# declare dates
  dates <- 
    tribble(
      ~resp_country_common, ~resp_round, ~resp_interview_start_date, ~resp_interview_end_date,
      "Armenia", "2009", "2009-09-16", "2009-10-14",
      "Azerbaijan", "2009", "2009-09-04", "2009-11-13",
      "Georgia", "2009", "2009-10-13", "2009-10-28",
      "Armenia", "2010", "2010-12-03", "2010-12-26",
      "Azerbaijan", "2010", "2010-11-15", "2010-12-25",
      "Georgia", "2010", "2010-11-09", "2010-11-30",
      "Armenia", "2012", "2012-11-02", "2012-11-29",
      "Azerbaijan", "2012", "2012-11-03", "2012-11-28",
      "Georgia", "2012", "2012-10-26", "2012-12-12",
      "Armenia", "2015", "2015-10-01", "2015-11-26",
      "Georgia", "2015", "2015-10-13", "2015-11-19",
      "Armenia", "2017", "2017-10-01", "2017-10-27",
      "Georgia", "2017", "2017-09-22", "2017-10-10",
      ) %>% 
    mutate(
      res_round = as.character(resp_round),
      resp_interview_start_date = as.Date(resp_interview_start_date),
      resp_interview_end_date = as.Date(resp_interview_end_date))

# merge in dates
  stacked_clean_w_missing_dates <- 
    stacked_clean %>%
    mutate(
      resp_interview_date = as.character(resp_interview_date),
      resp_interview_date = ifelse(as.Date(resp_interview_date) < as.Date("1999-12-29"), NA_character_, resp_interview_date),
      resp_interview_date = as.Date(resp_interview_date)) %>%
    left_join(dates, by = c("resp_country_common", "resp_round"))
```

# Save data

```{r}
  saveRDS(stacked_clean_w_missing_dates, "../cleaned-data/y-4-multi-caucasus-barometer.rds")
```
